class SievesController < ApplicationController
  def index
    list
    render :action => 'list'
  end

  def create
    sieve = Sieve.create_new_sieve(params[:sieve])
    unless sieve.errors.empty? 
      flash[:error] = "基金筛创建失败，请检查您的输入"
      redirect_to :action => "index"
      return
    end
    redirect_to :action => "show", :id => sieve.id
  end
  
  def show
    @sieve = Sieve.find(params[:id])
    @funds = @sieve.sieve
  end
  
  def new
    @sieve = Sieve.new
    @report_dates = MorningStarReport.report_dates
  end
  
  def list
    new
    @sieves = Sieve.find(:all).reverse
  end
  
  def combine
    begin 
      sieve = Sieve.combine(params[:ids].select{|key, value| value == "1"}.map{|pair| pair.first})
    rescue Exception
      flash[:error] = "组合基金筛失败，请检查您的输入"
      redirect_to :action => "index"
      return
    end
    redirect_to :action => "show", :id => sieve.id
  end
end
